home *** CD-ROM | disk | FTP | other *** search
- /* PROGRAM mac_callbk.c */
- /* This program will read the Callbook Database for TCP/IP sessions */
-
- /****************************************************/
- /* HISTORY */
- /* */
- /* JAN 9 1989 KGS - original release */
- /* JAN 12 1989 KGS - added raw mode */
- /* FEB 9 1989 DLH - changed to support TCP/IP */
- /****************************************************/
-
- #include <stdio.h>
- #include <ctype.h>
- #include "global.h"
- #include "config.h"
- #ifdef CALLBK
- #include "callbook.h"
- #include "timer.h"
- #include "mbuf.h"
- #include "internet.h"
- #include "icmp.h"
- #include "netuser.h"
- #include "tcp.h"
-
- extern FILE *cbklfp;
- extern FILE *cbkfp;
- extern char *logtime();
- static char record [RECORD_LENGTH];
- static char log_string[80];
- static char out_string[80];
- static long line_number = 0;
- static int ftflag = 0;
- static long callbook_size = 0;
-
- mac_callbk(tcb,who)
- struct tcb *tcb; /* tcb to send on */
- char *who;
- {
-
- if (cbkfp == NULLFILE) { /* open database */
- sprintf(out_string,"Unable to process TCP/IP Callbook Server request.\015\012");
- sndmsg(tcb,out_string);
- return;
- }
- if (ftflag)
- goto getcall;
-
- fseek(cbkfp,0L,2); /* seek to end of file */
- callbook_size = ftell(cbkfp); /* this is file size */
- ftflag = 1; /* set the flag for first time initialization */
-
- getcall:
- who++;
- uppercase(who,who);
- binary_search(who, CALLSIGN_START, CALLSIGN_LEN, callbook_size, tcb);
- return;
- }
-
- static
- print_log() /* send log output to file */
- {
- if(cbklfp == NULLFILE)
- return; /* return if no log file */
- fprintf (cbklfp, "%s", log_string);
- } /* end print_log */
-
- static
- binary_search(string, start, length, callbook_size, tcb)
- int start;
- int length;
- char string[RECORD_LENGTH];
- long callbook_size;
- struct tcb *tcb; /* tcb to send on */
-
- {
- int i = 0;
- long low = 0;
- long middle = 0;
- long high = 0;
- int modified = 0;
- char callsign_mod[CALLSIGN_LEN];
- static char spaces[] = " ";
-
- for (i = 0; i < strlen(string); i++) { /* mod callsign to match database */
- if (isdigit (string[i])) {
- strcpy (callsign_mod, (string + i));
- strncat (callsign_mod, spaces, length - strlen(string));
- strncat (callsign_mod, string, i);
- ++modified;
- }
- }
- if (!modified) {
- strcpy(callsign_mod,string); /* special case with no numbers */
- }
- high = callbook_size / RECORD_LENGTH; /* how many records in database */
- do {
- middle = (low + high) / 2;
- fseek (cbkfp, (middle * RECORD_LENGTH), 0);
- fgets (record,RECORD_LENGTH,cbkfp);
- ++line_number;
- if (strncmp(callsign_mod, (record + start), CALLSIGN_LEN) < 0) {
- high = middle - 1;
- }
- else {
- if (strncmp(callsign_mod, (record + start), CALLSIGN_LEN) > 0) {
- low = middle + 1;
- }
- }
- }
- while ((strncmp(callsign_mod, (record + start), CALLSIGN_LEN) != 0) && (low <= high));
- if (low <= high) {
- sprintf(log_string,"[%s] TCP/IP Callbook request by %s for %s.\n",
- logtime(),psocket(&tcb->conn.remote),string);
- print_log();
- format(tcb);
- }
- else {
- sprintf(log_string,"[%s] TCP/IP Callbook request by %s for %s not completed.\n",
- logtime(),psocket(&tcb->conn.remote),string);
- print_log();
- sprintf(out_string,"Callbook record NOT found for ->%s<-.\015\012",string);
- sndmsg(tcb,out_string);
- }
- } /* end binary_search */
-
- static
- uppercase(to, from)
- char *to;
- char *from;
-
- { /* copys a lower case string to uppercase */
- while (strncmp (from, "\0", 1) != 0) {
- if (islower(*from)) { /* if lower case, copy as uppercase */
- *to = toupper(*from);
- }
- else {
- *to = *from; /* already uppercase, copy as is */
- }
- if (strncmp (to, "_", 1) == 0) { /* convert underscore to space */
- strncpy (to, " ", 1);
- }
- ++to;
- ++from;
- }
- *to = *from; /* copy \0 at end of string */
- } /* end uppercase */
-
- static
- format(tcb)
- struct tcb *tcb; /* tcb to send on */
- {
- char string[RECORD_LENGTH];
- char mod_string[RECORD_LENGTH];
- static char eos[] = "\0"; /* end of string char */
- char *string_end,*zip;
-
- sprintf(out_string,"\015\012"); /* insure we start on a new line */
- sndmsg(tcb,out_string);
- strncpy (string, (record + FIRST_NAME_START), FIRST_NAME_LEN);
- strcpy ((string + FIRST_NAME_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"Name: %.11s",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + MIDDLE_INIT_START), MIDDLE_INIT_LEN);
- strcpy ((string + MIDDLE_INIT_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string," %.1s. ",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + LAST_NAME_START), LAST_NAME_LEN);
- strcpy ((string + LAST_NAME_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"%.20s ",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + LAST_NAME_SUFF_START), LAST_NAME_SUFF_LEN);
- strcpy ((string + LAST_NAME_SUFF_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"%s\015\012",string);
- sndmsg(tcb,out_string);
-
- strncpy (string, (record + CALLSIGN_START), CALLSIGN_LEN);
- strcpy ((string + CALLSIGN_LEN), eos);
- string_end = (string + strlen (string)) - 1; /* fix the callsign so it */
- while (strncmp (string_end, " ", 1) != 0) { /* is readable */
- --string_end;
- }
- strcpy (mod_string, (string_end + 1));
- strncat (mod_string, string, ((strlen (string)) -(strlen (string_end))));
- rm_whitespace(mod_string);
- sprintf(out_string,"License: %.8s ",mod_string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + LICENSE_CLASS_START), LICENSE_CLASS_LEN);
- strcpy ((string + LICENSE_CLASS_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string," License Class: %s\015\012",string);
- sndmsg(tcb,out_string);
-
- strncpy (string, (record + MAIL_ADRS_START), MAIL_ADRS_LEN);
- strcpy ((string + MAIL_ADRS_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"Mail address: %s, ",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + MAIL_ADRS_CTY_START), MAIL_ADRS_CTY_LEN);
- strcpy ((string + MAIL_ADRS_CTY_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"%s, ",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + MAIL_ADRS_ST_START), MAIL_ADRS_ST_LEN);
- strcpy ((string + MAIL_ADRS_ST_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"%s ",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + MAIL_ADRS_ZIP_START), MAIL_ADRS_ZIP_LEN);
- strcpy ((string + MAIL_ADRS_ZIP_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"%.5s-%.4s\015\012",string,
- zip = "0000");
- sndmsg(tcb,out_string);
-
- strncpy (string, (record + STA_ADRS_START), STA_ADRS_LEN);
- strcpy ((string + STA_ADRS_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"Station address: %s, ",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + STA_ADRS_CTY_START), STA_ADRS_CTY_LEN);
- strcpy ((string + STA_ADRS_CTY_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"%s, ",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + STA_ADRS_ST_START), STA_ADRS_ST_LEN);
- strcpy ((string + STA_ADRS_ST_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"%s\015\012",string);
- sndmsg(tcb,out_string);
-
- strncpy (string, (record + EFFECTIVE_START), EFFECTIVE_LEN);
- strcpy ((string + EFFECTIVE_LEN), eos);
- rm_whitespace(string);
- date (string);
- sprintf(out_string,"Effective date: %s ",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + EXPIRATION_START), EXPIRATION_LEN);
- strcpy ((string + EXPIRATION_LEN), eos);
- rm_whitespace(string);
- date (string);
- sprintf(out_string," Expiration date: %s\015\012",string);
- sndmsg(tcb,out_string);
-
- strncpy (string, (record + PREVIOUS_CALLSIGN_START), PREVIOUS_CALLSIGN_LEN);
- strcpy ((string + PREVIOUS_CALLSIGN_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string,"Previous Callsign: %.8s",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + PREVIOUS_CLASS_START), PREVIOUS_CLASS_LEN);
- strcpy ((string + PREVIOUS_CLASS_LEN), eos);
- rm_whitespace(string);
- sprintf(out_string," Previous Class: %s\015\012",string);
- sndmsg(tcb,out_string);
-
- strncpy (string, (record + BIRTH_START), BIRTH_LEN);
- strcpy ((string + BIRTH_LEN), eos);
- rm_whitespace(string);
- date (string);
- sprintf(out_string,"Birthdate: %.13s ",string);
- sndmsg(tcb,out_string);
- strncpy (string, (record + PROCESS_START), PROCESS_LEN);
- strcpy ((string + PROCESS_LEN), eos);
- rm_whitespace(string);
- date (string);
- sprintf(out_string," Process date: %s\015\012\015\012",string);
- sndmsg(tcb,out_string);
- }
-
- static
- rm_whitespace(string)
- char *string;
- {
- int i = 0;
- static char eos[] = "\0"; /* end of string char */
- char *string_end;
-
- string_end = (string + strlen (string)) - 1;
- while (strncmp (string_end, " ", 1) == 0) {
- --string_end;
- }
- strcpy ((string_end + 1), eos);
- }
-
- static
- date(string)
- char *string;
- {
- int day = 0;
- int month = 0;
- int year = 0;
- int leap = 0;
- char temp_string[RECORD_LENGTH];
- static char eos[] = "\0";
- static int days_per_month [2][13] = {
- {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
- {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
- };
-
- day = atoi(string + 2); /* get the day of year */
- strcpy ((string + 2), eos);
- year = atoi(string); /* get the year */
- if (year < THISYEAR + 10) {
- year += 1900;
- }
- else {
- year += 1800;
- }
- leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
- for (month = 1; day > days_per_month [leap][month]; month++) {
- day -= days_per_month [leap][month];
- }
-
- switch (month) {
- case 1 :
- strcpy(string, "Jan.");
- break;
-
- case 2 :
- strcpy(string, "Feb.");
- break;
-
- case 3 :
- strcpy(string, "Mar.");
- break;
-
- case 4 :
- strcpy(string, "Apr.");
- break;
-
- case 5 :
- strcpy(string, "May");
- break;
-
- case 6 :
- strcpy(string, "Jun.");
- break;
-
- case 7 :
- strcpy(string, "Jul.");
- break;
-
- case 8 :
- strcpy(string, "Aug.");
- break;
-
- case 9 :
- strcpy(string, "Sep.");
- break;
-
- case 10 :
- strcpy(string, "Oct.");
- break;
-
- case 11 :
- strcpy(string, "Nov.");
- break;
-
- case 12 :
- strcpy(string, "Dec.");
- break;
-
- default :
- sprintf (log_string,"Invalid month ->%d<- .\n", month);
- print_log ();
- break;
- }
- sprintf (temp_string,"%s %d, %d", string, day, year);
- strcpy (string, temp_string);
- }
- #endif
-